home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / unix / arcunx11 / arc.sh1 / arcadd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-01  |  6.7 KB  |  202 lines

  1. /*
  2.  *    arcadd.c    1.1
  3.  *
  4.  *    Author: Thom Henderson
  5.  *    Original System V port: Mike Stump
  6.  *    Enhancements, Bug fixes, and cleanup: Chris Seaman
  7.  *    Date: Fri Mar 20 09:57:02 1987
  8.  *    Last Mod.    3/21/87
  9.  *
  10.  */
  11.  
  12. /*
  13.  * ARC - Archive utility - ARCADD
  14.  * 
  15.  * Version 3.39, created on 02/05/86 at 22:21:53
  16.  * 
  17.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  18.  * 
  19.  *     Description:
  20.  *          This file contains the routines used to add files to an archive.
  21.  */
  22.  
  23. #include "arc.h"
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26.  
  27. INT addarc(argc,argv,move,update,fresh)/* add files to archive */
  28. INT argc;                              /* number of arguments */
  29. char *argv[];                          /* pointers to arguments */
  30. INT move;                              /* true if moving file */
  31. INT update;                            /* true if updating */
  32. INT fresh;                             /* true if freshening */
  33. {
  34.     char *buf;                         /* pathname buffer */
  35.     char *i, *strrchr();               /* string indexing junk */
  36.     INT n;                             /* indices */
  37.     struct stat fbuf;                  /* file information structure */
  38.     struct heads hdr;                  /* file header data storage */
  39.     INT addfile();
  40.  
  41.     openarc(1);                        /* open archive for changes */
  42.  
  43.     for (n=0; n<argc; ++n)
  44.     {
  45.         stat(argv[n],&fbuf);           /* find out what kind of file it is */
  46.  
  47.         /* if file is a regular file, try to add it */
  48.         if (((fbuf.st_mode >> 12) == 8) || ((fbuf.st_mode >> 12) == 0))
  49.         {
  50.             if (i=strrchr(buf=argv[n], '/')) buf=i+1;
  51.             addfile(argv[n],buf,update,fresh);
  52.         }
  53.         else
  54.         {
  55.             printf("Invalid file type: %s\n",argv[n]);
  56.             ++nerrs;
  57.         }
  58.     }
  59.  
  60.     /* quit if no changes to make */
  61.     if (nerrs == argc)
  62.     {
  63.         fclose(arc);
  64.         fclose(new);
  65.         abort("I have no work to do!");
  66.     }
  67.  
  68.     /* now we must copy over all files that follow our additions */
  69.  
  70.     while (readhdr(&hdr,arc))          /* while more entries to copy */
  71.     {
  72.         writehdr(&hdr,new);
  73.         filecopy(arc,new,hdr.size);
  74.     }
  75.  
  76.     hdrver = 0;                        /* archive EOF type */
  77.     writehdr(&hdr,new);                /* write out our end marker */
  78.     closearc(1);                       /* close archive after changes */
  79.  
  80.     if (move) for (n=0; n<argc; ++n)   /* if this was a move */
  81.     {
  82.         stat(argv[n],&fbuf);           /* make sure file can be removed */
  83.         if (((fbuf.st_mode >> 12) == 8) || ((fbuf.st_mode >> 12) == 0))
  84.         {
  85.             if (unlink(argv[n]) && warn)
  86.             {
  87.                 printf("Cannot unsave %s\n",argv[n]);
  88.                 ++nerrs;
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. static INT addfile(path,name,update,fresh) /* add named file to archive */
  95. char *path;                            /* path name of file to add */
  96. char *name;                            /* name of file to add */
  97. INT update;                            /* true if updating */
  98. INT fresh;                             /* true if freshening */
  99. {
  100.     struct heads nhdr;                 /* data regarding the new file */
  101.     struct heads ohdr;                 /* data regarding an old file */
  102.     FILE *f, *fopen();                 /* file to add, opener */
  103.     long starts, ftell();              /* file locations */
  104.     INT upd = 0;                       /* true if replacing an entry */
  105.  
  106.     if (!(f=fopen(path,"r")))          /* quit if we can't open the file */
  107.     {
  108.         if (warn)
  109.             printf("Cannot read file: %s\n",path);
  110.         nerrs++;
  111.         fclose(f);
  112.         return;
  113.     }
  114.  
  115.     /* fill the header structure with information about the new file */
  116.  
  117.     /* save filename, using 14 or 12 char template, based on ibmpc option */
  118.     strncpy(nhdr.name,name,((ibmpc) ? FNLEN2 : FNLEN1)-1);
  119.     nhdr.size = 0;                     /* clear out size storage */
  120.     nhdr.crc = 0;                      /* clear out CRC check storage */
  121.     getstamp(f,&nhdr.date,&nhdr.time);
  122.  
  123.     /* position archive to spot for new file */
  124.  
  125.     if (arc)                           /* if adding to existing archive */
  126.     {
  127.         starts = ftell(arc);           /* where are we? */
  128.         while (readhdr(&ohdr,arc))     /* while more files to check */
  129.         {
  130.             if (strcmp(ohdr.name,nhdr.name) == 0)
  131.             {
  132.                 upd = 1;               /* replace existing entry */
  133.                 if (update || fresh)   /* if updating or freshening */
  134.                 {
  135.                     if (nhdr.date<ohdr.date ||
  136.                        (nhdr.date == ohdr.date && nhdr.time <= ohdr.time))
  137.                     {
  138.                         fseek(arc,starts,0);
  139.                         fclose(f);
  140.                         return;        /* skip if not newer */
  141.                     }
  142.                 }
  143.             }
  144.  
  145.             if (strcmp(ohdr.name,nhdr.name) >= 0)
  146.                 break;                 /* found our spot */
  147.  
  148.             writehdr(&ohdr,new);       /* entry preceeds update; keep it */
  149.             filecopy(arc,new,ohdr.size);
  150.             starts = ftell(arc);       /* now where are we? */
  151.         }
  152.  
  153.         if (upd)                       /* if an update */
  154.         {
  155.             if (note)
  156.             {
  157.                 printf("Updating file: %-14s  ",name);
  158.                 fflush(stdout);
  159.             }
  160.             fseek(arc,ohdr.size,1);
  161.         }
  162.         else if (fresh)                /* else if freshening */
  163.         {
  164.             fseek(arc,starts,0);       /* then do not add files */
  165.             fclose(f);
  166.             return;
  167.         }
  168.         else                           /* else adding a new file */
  169.         {
  170.             if (note)
  171.             {
  172.                 printf("Adding file:   %-14s  ",name);
  173.                 fflush(stdout);
  174.             }
  175.             fseek(arc,starts,0);       /* reset for next time */
  176.         }
  177.     }
  178.  
  179.     else                               /* no existing archive */
  180.     {
  181.         if (fresh)                     /* cannot freshen nothing */
  182.         {
  183.             fclose(f);
  184.             return;
  185.         }
  186.         else if (note)                 /* else adding a file */
  187.         {
  188.             printf("Adding file:   %-14s  ",name);
  189.             fflush(stdout);
  190.         }
  191.     }
  192.  
  193.     starts = ftell(new);               /* note where header goes */
  194.     hdrver = ARCVER;                   /* anything but end marker */
  195.     writehdr(&nhdr,new);               /* write out header skeleton */
  196.     pack(f,new,&nhdr);                 /* pack file into archive */
  197.     fseek(new,starts,0);               /* move back to header skeleton */
  198.     writehdr(&nhdr,new);               /* write out real header */
  199.     fseek(new,nhdr.size,1);            /* skip over data to next header */
  200.     fclose(f);                         /* all done with the file */
  201. }
  202.